/*
 * Copyright 2020 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "FreeRTOS.h"
#include "task.h"

#include "fsl_debug_console.h"

#include "pin_mux.h"
#include "board.h"

#include "fsl_gpio.h"
#include "fsl_port.h"

#include "fsl_inputmux_connections.h"
#include "fsl_inputmux.h"

#include "arm_math.h"
#include "arm_const_structs.h"

#include "app_main.h"

#include <imu_ops.h>
#include "ringbuffer.h"

//#include "TimeSeries.h"    /* 头文件包含*/
volatile float g_dbgFeature[APP_FEATURE_DIM] = {0.7f, 0.4f};
volatile float g_rmsDiv = APP_FEATURE_RMS_DIVIDER;
float hanning_ary[APP_FFT_LEN] = {0};
uint16_t ring_buffer[1];//[2 * 3 * APP_FEATURE_CNT];
ringbuffer_t ringbuffer_handler;

void delay_ms(uint32_t ms) {
	vTaskDelay(ms);
}

#include "TimeSeries.h"    /* 头文件包含*/

float data_input[TSS_INPUT_DATA_LEN * TSS_INPUT_DATA_DIM];
tss_status status;  /* 结构体声明 */
float probability;
int Receive_NUM = 0;
#define Predict_NUM		(TSS_INPUT_DATA_LEN * TSS_INPUT_DATA_DIM)    //  3 * 128
float Pre_Data[Predict_NUM];  /* 带预测样本数组 */

/***********************************************************************************/

#define DATA_INPUT_SIZE (3  *  128)    //  3轴  *  128个数据点
float data_input[3 * 128]; // 3个轴，每个轴128个样本
static int dataIndex  =  0;    //  当前数据存储位置索引


#define gather 0


void app_sensor_task(void *parameters) {



	int dataIndex = 0; // 用于跟踪data_input数组的索引



	// 初始化传感器
	if (IMU_Init()) {
		PRINTF("IMU Sensor init failed! \r\n");
		// 处理初始化失败的情况，这里简单地进入无限循环
		while (1) {
			vTaskSuspend(NULL); // 挂起任务
		}
	}
	PRINTF("IMU Sensor init sucess! \r\n");
	//进去模型初始化

	// 定义读取缓冲区
	int16_t readBuf[APP_SENSOR_TASK_READ_SIZE / 6][3];
// 初始化模型
	status = tss_ad_init();
	if (status != TSS_SUCCESS) {
		/* Handle the initialization failure cases */
		PRINTF("模型初始化失败!\r\n");
		return -1;
	}
	PRINTF("tss_ad_init ok!\r\n", status);

	while (1) {
		// 获取FIFO缓冲区中的数据项数量
		int fifoCnt = IMU_Get_Fifo_Cnt();
		// 计算要读取的数据量
		int maxItemCnt = APP_SENSOR_TASK_READ_SIZE / 6;
		int bytesToRead = maxItemCnt * 6;

		// 如果FIFO缓冲区中的数据足够，则读取数据
		if (fifoCnt * 6 >= bytesToRead) {
			int readSize = IMU_ReadSensorData(readBuf, fifoCnt, bytesToRead);
#if gather
			// 遍历读取到的每个数据点（每点3轴）
			for (int i = 0;  i  < readSize  / 6;  i++) {

				// 检查剩余空间是否足够存储3个数据（1个数据点）
				if  (dataIndex +  3  >  DATA_INPUT_SIZE) {
					//  执行预测
					float probability;
					int status = tss_ad_predict(data_input, &probability);
					if (probability >=  0.9) {
						PRINTF("fan  normal !\r\n");
						PRINTF("%f\r\n",probability);
						LED_GREEN_ON();
					} else {
						PRINTF("fan  clog  !\r\n");
						PRINTF("%f\r\n",probability);

						LED_GREEN_OFF();
					}
					dataIndex  =  0;  //  重置索引
				}

				// 将传感器数据存入  data_input
				data_input[dataIndex++]  =  (float)readBuf[i][0];  //  假设需要类型转换
				data_input[dataIndex++]  =  (float)readBuf[i][1];
				data_input[dataIndex++]  =  (float)readBuf[i][2];

			}
#else

            for (int i = 0; i < readSize / 6; i++)
            {
                PRINTF("%d %d %d\r\n", readBuf[i][0], readBuf[i][1], readBuf[i][2]);
            }
#endif /* gather */
		}
		// 任务延时，防止过度占用CPU资源
		vTaskDelay(200);
	}

}
